using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ERP.WarehouseManagement;
using ERP.WarehouseManagement.Dto;
using Microsoft.AspNetCore.Mvc;

namespace ERP.Web.Controllers.WarehouseManagement
{
    /// <summary>
    /// 库存交易统计控制器
    /// </summary>
    [Route("[controller]")]
    public class InventoryTransactionStatisticsController : Controller
    {
        private readonly IInventoryTransactionService _inventoryTransactionService;

        public InventoryTransactionStatisticsController(
            IInventoryTransactionService inventoryTransactionService)
        {
            _inventoryTransactionService = inventoryTransactionService;
        }

        /// <summary>
        /// 显示库存交易统计页面
        /// </summary>
        [HttpGet]
        [Route("")]
        [Route("Index")]
        public IActionResult Index()
        {
            // 明确指定视图路径
            return View("~/Views/WarehouseManagement/InventoryTransactionStatistics/Index.cshtml");
        }

        /// <summary>
        /// 获取库存交易统计数据
        /// </summary>
        [HttpPost]
        [Route("GetStatistics")]
        public async Task<JsonResult> GetStatistics([FromBody] InventoryTransactionQueryDto queryDto)
        {
            try
            {
                // 如果前端未提供查询月份，使用当前月份
                if (!queryDto.QueryMonth.HasValue)
                {
                    queryDto.QueryMonth = DateTime.Now;
                }

                var result = await _inventoryTransactionService.GetInventoryTransactionStatisticsAsync(queryDto);
                return Json(new { code = 0, msg = "成功", data = result });
            }
            catch (Exception ex)
            {
                // 记录异常信息
                Console.WriteLine($"获取库存交易统计数据异常: {ex}");
                return Json(new { code = 1, msg = ex.Message });
            }
        }

        /// <summary>
        /// 用于测试的模拟数据接口（当后端服务不可用时使用）
        /// </summary>
        [HttpPost]
        [Route("GetStatisticsMock")]
        public JsonResult GetStatisticsMock([FromBody] InventoryTransactionQueryDto queryDto)
        {
            // 创建模拟数据
            var result = new InventoryTransactionStatisticsResult();
            var random = new Random();
            
            // 模拟一个月的数据
            var date = queryDto.QueryMonth ?? DateTime.Now;
            var daysInMonth = DateTime.DaysInMonth(date.Year, date.Month);
            
            for (int i = 1; i <= daysInMonth; i++)
            {
                var dayDate = new DateTime(date.Year, date.Month, i);
                var inbound = random.Next(50, 200);
                var outbound = random.Next(30, 180);
                
                result.DailyStatistics.Add(new InventoryTransactionStatisticsDto
                {
                    Date = dayDate.ToString("yyyy-MM-dd"),
                    InboundQuantity = inbound,
                    OutboundQuantity = outbound
                });
            }
            
            return Json(new { code = 0, msg = "成功", data = result });
        }
    }
} 